<?php
namespace app\rbac\model;
use think\Validate;
use think\Db;
use app\base\model\Base;
class RoleM extends Base
{

       protected function _V($scene,$data)
       {
           $rule = [
               'role_name' => 'require',
               'id' => 'require|number',
           ];
        $msg = [
        'role_name.require' => '角色名称不能为空',
        'id.require' => '角色ID不能为空',
        'id.number' => '角色ID只能是数字',
        ];
           $validate = new Validate($rule,$msg);
           $validate->scene('add', ['role_name']);
           $validate->scene('edit', ['role_name','id']);
           $result = $validate->scene($scene)->check($data);
           if($result === false)
           {
               return $validate->getError();
           }
           else
           {
               return true;
           }

       }

    /**
     * 新增数据
     * @return mixed
     */

    public function add($data)
    {
        $res = $this->_V('add',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $insertData = [];
        $res = Db::table('role')->insertGetId(['role_name'=>$data['role_name']]);
        if($res)
        {
            //权限
            foreach($data['pri_id'] as $k=>$v)
            {
                $insertData[$k]['role_id'] = $res;
                $insertData[$k]['pri_id'] = $v;
            }
            Db::table('role_pri')->insertAll($insertData);
            return true;
        }
        return false;
    }

    /**
     * 全部数据
     */
    public function lst()
    {
        $data = Db::table('role')
            ->alias('r')
            ->field('r.id,r.role_name,GROUP_CONCAT(p.pri_name) as pri_names')
            ->join('role_pri rp','rp.role_id = r.id','LEFT')
            ->join('privilege p','p.id = rp.pri_id','LEFT')
            ->group('r.id')
            ->where(['r.is_usable'=>1])
            ->select();
        return $data;
    }

    /**
     * 修改数据
     * @param $data
     * @return bool
     */
    public function edit($data)
    {
        $res = $this->_V('edit',$data);
        if($res !== true)
        {
            $this->code = -1;
            $this->msg = $res;
            return false;
        }
        $insertData = [];
        //权限
        foreach($data['pri_id'] as $k=>$v)
        {
            $insertData[$k]['role_id'] = $data['id'];
            $insertData[$k]['pri_id'] = $v;
        }

        // 启动事务
        Db::startTrans();
        try{
            Db::table('role')->where('id',$data['id'])->update(['role_name'=>$data['role_name']]);
            Db::table('role_pri')->where('role_id',$data['id'])->delete();
            Db::table('role_pri')->insertAll($insertData);
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            $this->code = -1;
            $this->msg = $e->getMessage();
            Db::rollback();
            return false;
        }
    }

    /**
     * 删除数据
     * @param $id
     */
    public function del($id)
    {
        // 启动事务
        Db::startTrans();
        try{
            Db::table('role')->where('id',$id)->delete();
            Db::table('role_pri')->where('role_id',$id)->delete();
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            $this->code = -1;
            $this->msg = $e->getMessage();
            Db::rollback();
            return false;
        }
    }
}
